package com.example.administrator.base.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.example.administrator.base.baseActivity.BaseActivity;
import com.example.administrator.base.bean.ReLogout;
import com.example.administrator.base.R;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.SharedPreferencesUtils;
import java.io.IOException;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class AddpointTwo extends BaseActivity {

    @InjectView(R.id.Toolbar)
    android.support.v7.widget.Toolbar Toolbar;
    @InjectView(R.id.ck_isline)
    CheckBox ckIsline;
    @InjectView(R.id.et_phone)
    EditText etPhone;
    @InjectView(R.id.et_code)
    EditText etCode;
    @InjectView(R.id.rb_no)
    RadioButton rbNo;
    @InjectView(R.id.rb_point)
    RadioButton rbPoint;
    @InjectView(R.id.rb_un)
    RadioButton rbUn;
    @InjectView(R.id.rb_one)
    RadioButton rbOne;
    @InjectView(R.id.btn_sure)
    Button btnSure;
    @InjectView(R.id.rg)
    RadioGroup rg;
    String type="";
    String  lat,lon;
    SweetAlertDialog sweetAlertDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addpoint_two);
        ButterKnife.inject(this);
        sweetAlertDialog = new SweetAlertDialog(this, 5);
        sweetAlertDialog.setCanceledOnTouchOutside(false);
        sweetAlertDialog.setTitleText("加载中");
        Toolbar.setTitle("赳猎人");
        Toolbar.setSubtitle("上传定位点");
        setSupportActionBar(Toolbar);
        Toolbar.setNavigationIcon(R.drawable.backbar);
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent=getIntent();
        lat=intent.getStringExtra("lat");
        lon=intent.getStringExtra("lon");
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == rbNo.getId()) {
                    type="1";
                } else if (checkedId == rbPoint.getId()) {
                    type="4";
                }else if (checkedId == rbUn.getId()){
                    type="5";
                }else if (checkedId == rbOne.getId()){
                    type="3";
                }

            }
        });
    }

    @Override
    protected void onResponseSuccess(Response response, String action) {
                        ResponseBody body = response.body();
                        if (body != null) {
                            try {
                                String  string = body.string();
                                Log.i("22154512",string);
                                final ReLogout mse = GsonTools.changeGsonToBean(string, ReLogout.class);
                                if (mse != null) {
                                    if (mse.getCode() == 1) {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                showToast( "" + mse.getMessage());
                                                sweetAlertDialog.dismiss();
                                                finish();
                                            }
                                        });
                                    } else {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                showToast("" + mse.getMessage());
                                                sweetAlertDialog.dismiss();
                                            }
                                        });
                                    }
                                } else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            showToast("数据异常请重试");
                                            sweetAlertDialog.dismiss();
                                        }
                                    });
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    showToast("数据异常请重试");
                                    sweetAlertDialog.dismiss();
                                }
                            });
                        }

            }


    @Override
    protected void onResponseFailure(Throwable e, String action) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast("网络异常请重试");
                sweetAlertDialog.dismiss();
            }
        });
    }

    @OnClick(R.id.btn_sure)
    public void onViewClicked() {
        String isLine;
        String address = String.valueOf(SharedPreferencesUtils.getParam(AddpointTwo.this, "address", ""));
        if (ckIsline.isChecked()) {
            isLine = "1";
        } else {
            isLine = "0";
        }
        String name = etPhone.getText().toString();
        String number = etCode.getText().toString();

        if (name.equals("")){
            showToast("请输入停放点名称");
            return;
        }
        if (number.equals("")){
            showToast("请输入预估停放数");
            return;
        }

        if (type.equals("")){
            Toast.makeText(AddpointTwo.this,"请选择停放点类型",Toast.LENGTH_LONG).show();
        }else {
            sweetAlertDialog.show();
            String location = String.valueOf(SharedPreferencesUtils.getParam(AddpointTwo.this,"location3378",""));
            if (!location.equals("")){
                setLocation(address, lat, lon, number, name, isLine,type,location);
            }else {
                sweetAlertDialog.dismiss();
                showToast("您的定位信息不完整");
            }

        }
    }


    /*设置停放点*/
    public void setLocation(String address, String lat, String lon, String number, String name, String isLine,String type,String location) {
        final RequestBody formBody = new FormBody.Builder()
                .add("action", "LOCATINGSTOP")
                .add("latitude", lat)
                .add("longitude", lon)
                .add("address", address)
                .add("lineFlag", isLine)
                .add("areaId", String.valueOf(SharedPreferencesUtils.getParam(AddpointTwo.this, "areaId", "")))
                .add("adminId", String.valueOf(SharedPreferencesUtils.getParam(AddpointTwo.this, "adminId", "")))
                .add("estimateParkingNum", number)
                .add("pointName", name)
                .add("location",location)
                .add("childrenAreaType",type)
                .build();
        final Request request = new Request.Builder()
                .url("http://operate.99bicycle.com/OperateBicyclePro/operationperson?")
                .post(formBody)
                .build();
        postRequest(request,"LOCATINGSTOP");
    }
}
